package com.five.monkey.feign.es.controller;

import com.five.monkey.feign.es.service.UserElasticsearchService;
import com.five.monkey.vo.CommonVo;
import com.five.monkey.vo.PageVo;
import com.five.monkey.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 狻猊
 * @date 2021/8/10 下午5:19
 */
@RestController
@RequestMapping("/es/user")
public class ESUserController {

    @Autowired
    private UserElasticsearchService userElasticsearchService;

    @PostMapping("/save")
    public CommonVo<UserVo> saveToElasticsearch(@RequestBody UserVo userVo) {
        userVo = userElasticsearchService.save(userVo);
        return CommonVo.ok(userVo);
    }

    @GetMapping("/find/by/{id}")
    public CommonVo<UserVo> findByIdFromElasticsearch(@PathVariable("id") Long id) {
        UserVo userVo = userElasticsearchService.findById(id);
        return CommonVo.ok(userVo);
    }

    @DeleteMapping("/delete/by/{id}")
    public CommonVo<Boolean> deleteByIdFromElasticsearch(@PathVariable("id") Long id) {
        boolean flag = userElasticsearchService.deleteById(id);
        return CommonVo.ok(flag);
    }

    @GetMapping("/find/all")
    public CommonVo<List<UserVo>> findAllFromElasticsearch() {
        List<UserVo> voList = userElasticsearchService.findAll();
        return CommonVo.ok(voList);
    }

    @GetMapping("/page/by/{status}")
    public CommonVo<PageVo<UserVo>> pageByStatusFromElasticsearch(@PathVariable("status") Integer status, @RequestParam(value = "page") int page,
                                                                  @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        PageVo<UserVo> userVoPage = userElasticsearchService.findByStatus(status, page, size, null);
        return CommonVo.ok(userVoPage);
    }

    @GetMapping("/list/by/{status}")
    public CommonVo<List<UserVo>> listByStatusFromElasticsearch(@PathVariable("status") Integer status) {
        List<UserVo> userVoPage = userElasticsearchService.findByStatus(status, null);
        return CommonVo.ok(userVoPage);
    }
}
